package com.example.hutooldemo.myhutool;

import cn.hutool.core.util.XmlUtil;
import com.example.hutooldemo.model.CarListModel;
import com.example.hutooldemo.model.DepartCarResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class HutoolXmlTest {
    public static void main(String[] args) {
        String xmlData =
                "<result>\n" +
                        "\t<cars lineid=\"63002\">\n" +

                        "\t\t<car>\n" +
                        "\t\t\t<vehicle>沪A38606D</vehicle>\n" +
                        "\t\t\t<time>13:35</time>\n" +
                        "\t\t</car>\n" +

                        "\t\t<car>\n" +
                        "\t\t\t<vehicle>沪A01308D</vehicle>\n" +
                        "\t\t\t<time>13:45</time>\n" +
                        "\t\t</car>\n" +

                        "\t\t<car>\n" +
                        "\t\t\t<vehicle>沪A38505D</vehicle>\n" +
                        "\t\t\t<time>13:54</time>\n" +
                        "\t\t</car>\n" +

                        "\t</cars>\n" +
                        "</result>";


        //获取文档
        Document document = XmlUtil.parseXml(xmlData);
        //获取根节点
        Element elementG = XmlUtil.getRootElement(document);

        //方式1：使用根节点下的直接 转成对象
        DepartCarResult departCarResult = XmlUtil.xmlToBean(elementG, DepartCarResult.class);

        //方式2：获取 根节点下 cars 节点
        Element carsElement = XmlUtil.getElement(elementG, "cars");
        //cars 节点是个 List，每一个对象为 car
        CarListModel carListModel = XmlUtil.xmlToBean(carsElement, CarListModel.class);


    }
}
